package com.auxgroup.smarthome.inner.controller;

import com.auxgroup.bridge.app.inner.dto.FamilyCenterRelieveDto;
import com.auxgroup.bridge.app.inner.vo.FamilyCenterDeviceVo;
import com.auxgroup.bridge.app.inner.vo.FamilyShareListVo;
import com.auxgroup.bridge.app.inner.vo.ShareResultVo;
import com.auxgroup.bridge.app.inner.vo.ShareUserListVo;
import com.auxgroup.bridge.app.inner.vo.SubShareUserVo;
import com.auxgroup.smarthome.inner.service.DeviceShareInnerService;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/7/28 17:09.
 */
@Api(description = "设备分享", tags = "设备管理")
@RestController
@RequestMapping("/devices")
public class DeviceShareController {
    @Autowired
    private DeviceShareInnerService deviceShareInnerService;

    @ApiOperation(value = "设备分享（单个设备）", notes = "设备管理--设备分享")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "uid", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "userTag", value = "注册类型：1，家庭永久用户，2，好友8小时权限", paramType = "form", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "deviceIds", value = "设备id,可以选多个，用英文,隔开", paramType = "form", dataType = "string", required = true),
    })
    @PostMapping(value = "/sharing")
    public ApiResponse<ShareResultVo> shareDevice(@ApiIgnore String uid, @ApiIgnore Integer userTag, @ApiIgnore String deviceIds) {
        return deviceShareInnerService.createShare(uid, deviceIds, userTag);
    }

    @ApiOperation(value = "获取设备用户分享列表", notes = "deviceId不传则获取所有分享")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "query", dataType = "string")
    })
    @GetMapping(value = "/sharing/{shareUid}")
    public ApiResponse<List<ShareUserListVo>> getShareUserList(@PathVariable("shareUid") String shareUid, @RequestParam(value = "deviceId", required = false) String deviceId) {
        return deviceShareInnerService.getSharing(shareUid, deviceId);
    }

    @ApiOperation(value = "获取分享用户子用户列表", notes = "uid不传则获取所有的子用户")
    @GetMapping(value = "/sharing/{shareUid}/sub_user")
    public ApiResponse<List<SubShareUserVo>> getSubShareUserList(@PathVariable("shareUid") String shareUid) {
        return deviceShareInnerService.getSubShareUserList(shareUid);
    }

    @ApiOperation(value = "解除分享", notes = "设备管理--解除分享")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "appid", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "shareId", value = "分享Id", paramType = "form", dataType = "string", required = true),
    })
    @DeleteMapping(value = "/sharing")
    public ApiResponse relieveShare(@ApiIgnore String appid, @ApiIgnore String token, @ApiIgnore String shareId) {
        return deviceShareInnerService.relieveShare(appid, token, shareId);
    }

    @ApiOperation(value = "好友到期解除分享")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "appid", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "uid", value = "用户id", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "form", dataType = "string", required = true),
    })
    @DeleteMapping(value = "/sharing/friend")
    public ApiResponse relieveFriendShare(@ApiIgnore String appid, @ApiIgnore String token, @ApiIgnore String uid, @ApiIgnore String deviceId) {
        return deviceShareInnerService.relieveFriendShare(appid, token, uid, deviceId);
    }

    @ApiOperation(value = "扫描二维码分享", notes = "设备管理--扫描二维码分享")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appid", value = "appid", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "token", value = "用户登录token", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "uid", value = "分享Id", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "qrContent", value = "二维码内容", paramType = "form", dataType = "string", required = true),
    })
    @PutMapping("/sharing/qrcode")
    public ApiResponse scanSharing(@RequestParam("appid") String appid, @RequestParam("token") String token, @RequestParam("uid") String uid, @RequestParam("qrContent") String qrContent) {
        return deviceShareInnerService.scanSharing(appid, token, uid, qrContent);
    }

    @ApiOperation(value = "获取家庭中心-家人列表")
    @GetMapping("/sharing/family_center/{uid}/family")
    public ApiResponse<List<FamilyShareListVo>> getFamilySharing(@PathVariable("uid") String uid) {
        return ApiResponse.ok(deviceShareInnerService.getFamilySharing(uid));
    }

    @ApiOperation(value = "获取家庭中心-好友列表")
    @GetMapping("/sharing/family_center/{uid}/friends")
    public ApiResponse<List<FamilyShareListVo>> getFriendSharing(@PathVariable("uid") String uid) {
        return ApiResponse.ok(deviceShareInnerService.getFriendSharing(uid));
    }

    @ApiOperation(value = "获取家庭中心-用户分享-设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pUid", value = "分享者uid",  paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "uid", value = "uid",  paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "userTag", value = "用户标签：1：家人，2：好友",  paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "batchNo", value = "批次号",  paramType = "query", dataType = "string")
    })
    @GetMapping("/sharing/family_center/devices")
    public ApiResponse<List<FamilyCenterDeviceVo>> getUserShareDeviceList(String pUid, String uid, Integer userTag, String batchNo) {
        return ApiResponse.ok(deviceShareInnerService.getUserShareDeviceList(pUid, uid, userTag, batchNo));
    }

    @ApiOperation(value = "解除家庭中心-设备列表")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "appid", value = "appid",  paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "token", value = "平台访问token",  paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "pUid", value = "分享者uid",  paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "uid", value = "uid",  paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "userTag", value = "用户标签：1：家人，2：好友",  paramType = "form", dataType = "string", required = true),
//            @ApiImplicitParam(name = "batchNo", value = "批次号",  paramType = "form", dataType = "string"),
//            @ApiImplicitParam(name = "deviceId", value = "设备id",  paramType = "form", dataType = "string")
//    })
    @DeleteMapping("/sharing/family_center/devices/relieve")
    public ApiResponse relieveUserSharingByOwner(@ApiIgnore @Valid FamilyCenterRelieveDto fcRelieve) {
        return deviceShareInnerService.relieveUserSharingByOwner(fcRelieve.getAppid(), fcRelieve.getToken(), fcRelieve.getpUid(), fcRelieve.getUid(), fcRelieve.getUserTag(), fcRelieve.getBatchNo(), fcRelieve.getDeviceId());
    }

    @GetMapping("/sharing/get_tree_nodes/{deviceId}")
    public ApiResponse getTreeNode(@PathVariable("deviceId") String deviceId) {
        ApiResponse apiResponse = ApiResponse.ok(deviceShareInnerService.buildUserDeviceTreeNode(deviceId));
        return apiResponse;
    }

}
